package com.lovo.sheyingback.controller;

import com.lovo.sheyingback.bean.Function;
import com.lovo.sheyingback.bean.Role;
import com.lovo.sheyingback.bean.form.FunctionForm;
import com.lovo.sheyingback.bean.form.RoleForm;
import com.lovo.sheyingback.bean.form.UserForm;
import com.lovo.sheyingback.result.ResponseResult;
import com.lovo.sheyingback.service.FunctionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/functions")
@ResponseResult
@Tag(name = "权限控制器FunctionController")
public class FunctionController {
    @Autowired
    private FunctionService functionService;
    @GetMapping
    @Operation(summary = "显示所有权限")

    public Object listFunction(UserForm userForm){
        return functionService.listFunction(userForm.getCurrent(),userForm.getPageSize());
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查找权限")
    public Object getById(@PathVariable Integer id){
        return functionService.getById(id);
    }

    @PostMapping
    @Operation(summary = "增加权限")
    public Object save(@RequestBody @Valid FunctionForm functionForm){
        Function function = new Function();
        BeanUtils.copyProperties(functionForm,function);
        function.setCreateDate(LocalDateTime.now());
        function.setUpdateDate(LocalDateTime.now());
        return functionService.insert(function);

    }
    @PutMapping("/{id}")
    @Operation(summary = "修改权限")
    public Object update(@PathVariable Integer id,@RequestBody FunctionForm functionForm){
        Function function = new Function();
        function.setId(id);
        BeanUtils.copyProperties(functionForm,function);
        return functionService.update(function);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除权限")
    public Object delete(@PathVariable Integer id){
        return functionService.delete(id);
    }
}
